import { UploadFile } from './../../custom.d';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { DinnerService } from './dinner.service';
import { CreateDinnerDto } from './dto/create-dinner.dto';
import { UpdateDinnerDto } from './dto/update-dinner.dto';

@Controller('dinner')
export class DinnerController {
  constructor(private readonly dinnerService: DinnerService) {}

  @Post()
  @UseInterceptors(FileInterceptor('cover'))
  create(
    @UploadedFile() file: UploadFile,
    @Body() createDinnerDto: CreateDinnerDto,
  ) {
    return this.dinnerService.create(file, createDinnerDto);
  }
  // 全部查询
  @Get()
  findAll() {
    return this.dinnerService.findAll();
  }
  //分页查询
  @Get('page')
  findByPage(
    @Query('page', ParseIntPipe) page: number,
    @Query('take', ParseIntPipe) take: number,
  ) {
    let skip = (page - 1) * take;
    return this.dinnerService.findByPage(skip, take);
  }
  //查询总条目数
  @Get('count')
  findCount() {
    return this.dinnerService.findCount();
  }

  @Get(':id')
  findByType(@Param('id') id: number) {
    return this.dinnerService.findByType(id);
  }

  @Patch(':id')
  update(@Param('id') id: number, @Body() updateDinnerDto: UpdateDinnerDto) {
    return this.dinnerService.update(id, updateDinnerDto);
  }

  @Patch('cover/:id')
  @UseInterceptors(FileInterceptor('cover'))
  updateCover(@UploadedFile() file: UploadFile, @Param('id') id: number) {
    return this.dinnerService.updateCover(file, id);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.dinnerService.remove(+id);
  }
}
